public class code2 {
    public static void main(String[] args) {
        int n = 10;
        int[] arr = new int[n];
        //sum0为0 sum1=arr[0]
        //为什么要多开一个位置：因为sum[r+1]=sum[r]+arr[r]
        long[] sum = new long[n+1];
        for(int i=1;i<=n;i++) {
            sum[i] = sum[i-1]+arr[i-1];
        }
    }

    public static void main2(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();//数组大小
        int m = scan.nextInt();//查询组数
        int[] arr = new int [n];
        for(int i=0;i<n;i++) {
            arr[i] = scan.nextInt();
        }
        //开二维数组把1-5次方都计算一遍
        long mod = (long)1e9+7;
        long[][] sum = new long[6][n+1];
        //i代表k次方
        for(int i=1;i<6;i++) {
            for(int j=1;j<=n;j++) {
                sum[i][j]=sum[i][j-1]+(long)Math.pow(arr[j-1], i);
                sum[i][j] %= mod;
            }
        }
        //计算l-r也就是sum[r+1]-sum[l]
        for(int i=0;i<m;i++) {
            //左右是从下标1开始的
            int l = scan.nextInt()-1;
            int r = scan.nextInt()-1;
            int k = scan.nextInt();
            //这里加上mod是为了防止结果为负号
            long ret = sum[k][r+1]+mod-sum[k][l];
            ret%=mod;
            System.out.println(ret);
        }
    }
}
